<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>{{ popData.title }}</span>
    </div>
    <el-form :model="form" class="form" label-width="70px" label-position="right">
      <el-form-item label=" 公司名称">
        <el-input
          :disabled="popData.type === 2"
          v-model="form.companyName"
          placeholder="请填写 公司名称"
          class="inputWidth" />
      </el-form-item>
      <el-form-item label=" 省市区">
        <el-cascader
          :disabled="popData.type === 2"
          v-model="form.city"
          :props="props"
          clearable
          class="inputWidth"
        />
      </el-form-item>
      <el-form-item label=" 办公地址">
        <el-input
          :disabled="popData.type === 2"
          v-model="form.address"
          placeholder="请填写 办公地址"
          class="inputWidth"
        />
      </el-form-item>
      <el-row v-if="popData.type !== 2">
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" @click="edit">{{ loadingText }}</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <router-link :to="`/company/index`" style="margin-right: 5px">
              <el-button type="primary">取消</el-button>
            </router-link>
          </el-form-item>

        </el-col>
      </el-row>
    </el-form>
  </el-card>
</template>
<script>
import company from '@/api/company'
import sysProvinceRegions from '@/api/provinceRegions'
export default {
  props: ['popData'],
  data() {
    return {
      form: {},
      loadingText: '保存',
      props: {
        formRegions: {
          type: String,
          default: '30,356,3240'
        },
        lazy: true,
        lazyLoad(node, resolve) {
          sysProvinceRegions.getByParentId({ parentId: node.value }).then(resp => {
            const nodes = resp.data.map(item => ({
              value: item.id,
              label: item.name,
              leaf: item.level > 1
            }))
            resolve(nodes)
          })
        }
      }
    }
  },
  created() {
    const id = this.popData.id
    if (id) {
      this.form.id = id
      this.loadingText = '编辑'
      this.getDetailById(id)
    } else {
      this.loadingText = '保存'
    }
  },
  methods: {
    getDetailById(id) {
      company.getById(id).then(res => {
        this.form = res.data
        this.form.city = this.form.city.split(',').map(Number)
      })
    },
    edit() {
      this.form.city = this.form.city.join(',')
      company.edit(this.form).then(res => {
        this.$message.success(res.message)
      })
      this.$parent.closePop()
    }
  }
}
</script>
<style>
@import "../../styles/dialogForm.scss";
.form{
  margin: auto auto;
}
</style>
